﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Enterprise
{
    public partial class Edit : AdminPage
    {
        private Enterprise curEnterprise = new Enterprise();
        private string id = "";
        private string classno = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            id = (Request.QueryString["id"] == null) ? "" : Request.QueryString["id"].Trim();
            classno = (Request.QueryString["clsno"] == null) ? "" : Request.QueryString["clsno"].Trim();
            if (!IsPostBack)
            {
                //上次请求路径
                if (hidReferrer.Value.Trim() == "")
                {
                    Uri uri = Request.UrlReferrer;
                    if (uri != null)
                    {
                        hidReferrer.Value = uri.ToString();
                    }
                } 
                InitData();
            }
        }

        private void InitData()
        {

            if (id != "")
            {
                int tid = int.Parse(id);
                EnterpriseInfo model = curEnterprise.GetEnterpriseInfo(tid);
                if (model != null)
                {
                    txtTargetUrl.Text = model.name;
                    txtDescription.Text = model.description;
                    txtContent.Text = model.info;
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改企业失败，未找到相对应的企业！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改企业失败，未从正确的地址编辑！&z=no");
            }
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {

            if (id != "")
            {
                EnterpriseInfo model = new EnterpriseInfo();
                int tid = int.Parse(id);
                model.id = tid;
                model.name = txtTargetUrl.Text.Trim();
                model.description = txtDescription.Text.Trim();
                model.info = txtContent.Text.Trim();

                if (curEnterprise.Update(model))
                {
                    string refurl = hidReferrer.Value.Trim();
                    if (refurl == "" || refurl == this.Request.Path.Trim())
                    {
                        Response.Redirect("../Message.aspx?message=修改企业成功！" + "&z=yes");
                    }
                    else
                    {
                        Response.Redirect(refurl);
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改企业失败！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改企业失败，无效编辑！&z=no");
            }
        }
    }
}
